<template>
  <div  class="dashboard-editor-container">
    <el-row style="background: #fff;">
      <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
        <el-tab-pane key="安装软件" label="安装软件" name="安装软件" lazy>
          <Software ref="安装软件"></Software>
        </el-tab-pane>
        <el-tab-pane key="非法软件" label="非法软件" name="非法软件" lazy>
          <illegalSoftware ref="非法软件"></illegalSoftware>
        </el-tab-pane>
        <el-tab-pane key="缺失软件" label="缺失软件" name="缺失软件" lazy>
          <compulsorySoftwareInstallation ref="缺失软件"></compulsorySoftwareInstallation>
        </el-tab-pane>
        <el-tab-pane key="软件统计" label="软件统计" name="软件统计" lazy>
          <softwareStatistics ref="软件统计"></softwareStatistics>
        </el-tab-pane>
        <!-- <el-tab-pane key="软件字典" label="软件字典" name="软件字典" lazy>
          <softwareDictionary ref="软件字典"></softwareDictionary>
        </el-tab-pane> -->
      </el-tabs>
    </el-row>
  </div>
</template>
<script>
import { connrequest } from '@/api/firstPage'
import  Software  from './Software.vue'
import  illegalSoftware  from './illegalSoftware.vue'
import  compulsorySoftwareInstallation  from './compulsorySoftwareInstallation.vue'
import  softwareStatistics  from './softwareStatistics.vue'
import  softwareDictionary  from '../system/softwareDictionary.vue'
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
export default {
  components: { Software, illegalSoftware, compulsorySoftwareInstallation, softwareStatistics, softwareDictionary },
  directives: { elDragDialog },
data() {
  return {
    activeName: '安装软件',
  }
},
  //生命周期 - 创建完成（访问当前this实例）
  created() {
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.$refs['安装软件'].getData()
  },
  methods: {
    handleClick(node){
      this.$nextTick(()=>{
        this.$refs[node.name].getData()
      })
    },
    addResources(){
      this.Action = 1
      this.resourceForm = {}
      this.$nextTick(() => {
        this.$refs['resourceForm'].clearValidate()
        this.fileList = []
      })
      this.dialogVisible = true
    },
    changeMailGlobal(row){
      this.Action = 2
      this.resourceForm = {...row}
      this.dialogVisible = true
    },
    deleteMailGlobal(row){
      this.Action = 3
      this.$confirm('此操作将删除该资源, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const data = {
          Data:{
            Action: this.Action,
            Detail: {
              AutoID: row.AutoID
            }
          }
        }
        const res = await connrequest('EditExactEMail', data)
        if(res.Status.StatusCode === 0){
          this.$message.success('删除成功')
          this.dialogVisible = false
          this.getData()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    async getData() {
      this.GetDictSoftwareTree()
      // const res = await connrequest('GetDeviceRealPerfStatus')
      // let now = Math.round(new Date().getTime()/1000).toString() * 1
      // res.Data = res.Data || []
      // res.Data.map(item => {
      //   if(item.Status !== 0){
      //     let time = item.Status === 1 ? now / 1000 - item.StartTime : item.EndTime - item.StartTime
      //     let d = Math.floor(time / 3600 / 24) < 1 ? '' : Math.floor(time / 3600 / 24) + '天';
      //     let h = Math.floor((time  % 86400) / 3600) < 1 ? '' :  Math.floor((time  % 86400) / 3600)  + '小时';
      //     let m = Math.floor(((time % 86400) % 3600) / 60) < 1 ? '' : Math.floor(((time % 86400) % 3600) / 60)  + '分钟';
      //     let s = Math.floor(((time % 86400) % 3600) % 60) < 1 ? '' : Math.floor(((time % 86400) % 3600) % 60)  + '秒';
      //     item.interval = `${d}${h}${m}${s}`;
      //   }
      //   item.TransTime = parseTime(item.TransTime)
      //   item.StartTime = parseTime(item.StartTime)
      //   item.EndTime = parseTime(item.EndTime)
      //   return item
      // })
      // this.tableData = res.Data
    },
    async GetDictSoftwareTree(){
      const res = await connrequest('GetDictSoftwareTree')
    },
    submit(){
      this.$refs['resourceForm'].validate(async (valid) => {
        if(valid){
          if(!this.resourceForm.resourceContent){
            this.$message.error('资源内容不能为空')
            return false
          }else{
            let val = this.resourceForm.resourceContent.replace(/(\n[\s\t]*\r*\n)/g, '\n').replace(/^[\n\r\n\t]*|[\n\r\n\t]*$/g, '')
            this.$set(this.resourceForm, 'resourceContent', val);
            if(!this.resourceForm.resourceContent){
              this.$message.error('资源内容不能为空')
              return false
            }
            const data = {
              Data:{
                Action: this.Action,
                Detail: {
                  TaskName: this.resourceForm.Descp,
                  Content: Buffer.from(this.resourceForm.resourceContent, 'utf-8').toString('base64')
                },
              }
            }
            const res = await connrequest('EditExactEMail', data)
            if(res.Status.StatusCode === 0){
              this.Action === 1 ? this.$message.success('添加成功') : this.$message.success('修改成功')
              this.dialogVisible = false
              this.getData()
            }
          }
        }
      })
    },
    async checkMailGlobal(row){
      let name = row.FileName.split('/').at(-1)
      window.open(`${row.FromType === 1 ? '/share/海关数据/' + row.TaskName : '/share/' + name}`)
    },
    filechange(file){
      if (!/\.(txt)$/.test(file.name) ) { 
        this.$message.error('请选择txt格式的文件')
        this.$refs['resourceForm'].clearValidate()
        this.fileList = []
        return false
      }
      else if(file.size > 50000){
        this.$message.error('请选择小于50k的文档')
        this.$refs['resourceForm'].clearValidate()
        this.fileList = []
        return false
      }
      let that = this
      var reader = new FileReader();
      reader.readAsDataURL(file.raw);
      reader.onload = function(e)
      {
        that.ImgBase64 = this.result.split(',')[1]
        let resourceContent = Buffer.from(that.ImgBase64, 'base64').toString('utf8')
        resourceContent = resourceContent.replace(/(\n[\s\t]*\r*\n)/g, '\n').replace(/^[\n\r\n\t]*|[\n\r\n\t]*$/g, '')
        that.$set(that.resourceForm, 'resourceContent', resourceContent);
        that.fileList = []
      }
    },
    uploadFile(){},
    async handleRemove(file, fileList) {
      const data = {
        Data:{
          InnerName: file.uid
        }
      }
      const res = await connrequest('DeleteEMailAttach', data)
      if(res.Status.StatusCode === 0){
        this.$message.success('文件删除成功')
        this.ImgBase64 = ''
      }
    },
    async handlePreview(file) {
      // const data = {
      //   Data:{
      //     FileName: file.InnerName
      //   }
      // }
      // const res = await downloadfile('DownloadPublicFile', data)
      // let blobUrl = window.URL.createObjectURL(new Blob([res]));
      // const a = document.createElement('a');
      // a.style.display = 'none';
      // a.download = file.InnerName;
      // a.href = blobUrl;
      // a.click();
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    }
  }
}
</script>
<style lang="scss" scoped>
/* @import url(); 引入css类 */
@import "~@/styles/variables.scss";
.content {
  position: relative;
  margin-top: 25px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  ::v-deep .certification{
    .el-form-item{
      margin-bottom: 10px;
    }
    .el-col{
      display: flex;
      align-items: center;
      p{
        width: 100px;
        display:block;
        color: #aaa;
        font-size: 12px;
        padding-left: 20px;
        white-space: nowrap;
      }
    }
  }
}
.top{
  flex-wrap: nowrap;
  margin-bottom: 20px;
}
.btn{
  margin: 20px 0;
}


.key-word-textarea {
  height: 170px;
  overflow: hidden;
  box-shadow: 0px -1px 5px 0px rgba(220, 229, 255, 0.7);
  border: 1px solid #D3D5E0;
  border-top: none;
  align-items: flex-start;
  .el-textarea ::v-deep {
    .el-textarea__inner {
      border: none;
      border-radius: 0;
      line-height: 20px;
      font-size: 14px;
      height: 172px !important;
      min-height: 172px !important;
    }
  }
}


.text-area-num-scroll {
  width: 28px;
  overflow: hidden;
  height: 180px;
  background: #F5F6FA;
  padding-top: 5px;
}
.text-area-num-box {
  min-height: 172px;
  overflow-y: auto;
  height: 172px;
  width: 48px;
  padding-bottom: 10px;
}
.text-area-num {
  line-height: 20px;
  width: 28px;
  text-align: center;
}
.flex {
  display: flex;
}
</style>